package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.ZhuangcheTimesDao;
import com.int_tech.MessageCodeAll.entity.ZhuangcheTimes;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.TimesPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/29.
 */
@Component
public class CheckZhuangcheGongshi {

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private ZhuangcheTimesDao zhuangcheTimesDao;

    /**
     * 装车工时校验
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkZhuangcheGongshi() throws ParseException {

        //获取已结束订单
        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(0,1);

//        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOverAndCarNumber(0,1,"043C4C155");

        for (Mession mession : messionList)
        {
            try
            {
                mession.setIsAnsyCarTime(1);
                messionDao.save(mession);

                //装车开始时间
                Date startOpenTime = mession.getStartOpenDoorTime();
                //装车结束时间
                Date startCloseTime = mession.getStartCloseDoorTime();

                //各个时间字符串日期
                String startOpenDay = DateTool.Date2String(startOpenTime,"yyyy-MM-dd");
                String startCloseDay = DateTool.Date2String(startCloseTime,"yyyy-MM-dd");

                //装车工时如果在同一天
                if(startCloseDay.equals(startOpenDay))
                {
                    //直接添加工时
                    List<Mession> messions = messionDao.findByEndOpenDoorTime(startOpenTime);

                    Mession thisMession = new Mession();

                    if(messions == null || messions.size() == 0)
                    {
                        thisMession = null;
                    }
                    else
                    {
                        thisMession = messions.get(0);
                    }

                    ZhuangcheTimes zhuangcheTimes = new ZhuangcheTimes();

                    float plus = 0;

                    if(thisMession == null)
                    {
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getStartCloseDoorTime(),mession.getStartOpenDoorTime(),1));
                        //直接添加工时
                    }
                    else
                    {
                        //工时减半
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getStartCloseDoorTime(),mession.getStartOpenDoorTime(),2));
                    }

                    zhuangcheTimes.setCarNumber(mession.getCarNumber());
                    zhuangcheTimes.setCreatedTime(new Date());
                    zhuangcheTimes.setDateString(startOpenDay);
                    zhuangcheTimes.setMessionNumber(mession.getMessionNumber());
                    zhuangcheTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                    zhuangcheTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                    zhuangcheTimes.setStatus(0);

                    zhuangcheTimes.setOnCarTime(plus);

                    zhuangcheTimesDao.save(zhuangcheTimes);
                }
                else
                {
                    //装车时间跨天了，进行跨天处理
                    List<Mession> messions = messionDao.findByEndOpenDoorTime(startOpenTime);

                    Mession thisMession = new Mession();

                    if(messions == null || messions.size() == 0)
                    {
                        thisMession = null;
                    }
                    else
                    {
                        thisMession = messions.get(0);
                    }

                    ZhuangcheTimes zhuangcheTimes = new ZhuangcheTimes();

                    String startEndTime = startOpenDay + " 23:59:59";
                    String endStartTIme = startCloseDay + " 00:00:00";

                    Date dStartEndTime = DateTool.string2Date(startEndTime,"yyyy-MM-dd HH:mm:ss");
                    Date dEndStartTime = DateTool.string2Date(endStartTIme,"yyyy-MM-dd HH:mm:ss");

                    float plus = 0;

                    if(thisMession == null)
                    {
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(dStartEndTime,mession.getStartOpenDoorTime(),1));
                        //直接添加工时
                    }
                    else
                    {
                        //工时减半
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(dStartEndTime,mession.getStartOpenDoorTime(),2));
                    }

                    zhuangcheTimes.setCarNumber(mession.getCarNumber());
                    zhuangcheTimes.setCreatedTime(new Date());
                    zhuangcheTimes.setDateString(startOpenDay);
                    zhuangcheTimes.setMessionNumber(mession.getMessionNumber());
                    zhuangcheTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                    zhuangcheTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                    zhuangcheTimes.setStatus(0);

                    zhuangcheTimes.setOnCarTime(plus);

                    zhuangcheTimesDao.save(zhuangcheTimes);


                    //第二天工时
                    zhuangcheTimes = new ZhuangcheTimes();


                    plus = 0;

                    if(mession == null)
                    {
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getStartCloseDoorTime(),dEndStartTime,1));
                        //直接添加工时
                    }
                    else
                    {
                        //工时减半
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getStartCloseDoorTime(),dEndStartTime,2));
                    }

                    zhuangcheTimes.setCarNumber(mession.getCarNumber());
                    zhuangcheTimes.setCreatedTime(new Date());
                    zhuangcheTimes.setDateString(startCloseDay);
                    zhuangcheTimes.setMessionNumber(mession.getMessionNumber());
                    zhuangcheTimes.setStartOpenDoorTime(mession.getStartOpenDoorTime());
                    zhuangcheTimes.setStartCloseDoorTime(mession.getStartCloseDoorTime());
                    zhuangcheTimes.setStatus(0);

                    zhuangcheTimes.setOnCarTime(plus);

                    zhuangcheTimesDao.save(zhuangcheTimes);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();

                //装车开关门工时计算异常
                mession.setIsAnsyCarTime(-1);
                messionDao.save(mession);
            }
        }
    }
}
